package com.example.liuxing.jdmall.fragement;

import android.app.Application;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.liuxing.jdmall.JDApplication;
import com.example.liuxing.jdmall.R;
import com.example.liuxing.jdmall.activity.LoginActivity;
import com.example.liuxing.jdmall.constant.IdiyMessage;
import com.example.liuxing.jdmall.controller.BaseController;
import com.example.liuxing.jdmall.controller.LoginController;
import com.example.liuxing.jdmall.listener.IModeChangeListener;
import com.example.liuxing.jdmall.utils.ActivityUtils;

/**
 * Created by liuxing on 2017/7/18.
 */

public class MineFragment extends BaseFragment implements View.OnClickListener, IModeChangeListener {

    private TextView userNameTv;
    private TextView userLvTv;

    private TextView userPayTv;
    private TextView userReciveTv;

    // 返回的结果
    @Override
    protected void handlerMessage(Message msg) {
        super.handlerMessage(msg);
        switch (msg.what) {
            case IdiyMessage.CLEAR_USER_ACTION_RESULT:
                getActivity().finish();
                ActivityUtils.start(getActivity(), LoginActivity.class);
                break;
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_mine, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initUI();
        initController();
    }

    @Override
    protected void initUI() {
        getActivity().findViewById(R.id.logout_btn).setOnClickListener(this);
        // 找到控件
        userNameTv = (TextView)getActivity().findViewById(R.id.user_name_tv);
        userLvTv = (TextView)getActivity().findViewById(R.id.user_level_tv);

        userPayTv = (TextView)getActivity().findViewById(R.id.wait_pay_tv);
        userReciveTv = (TextView)getActivity().findViewById(R.id.wait_receive_tv);

        // 请求
        JDApplication application = (JDApplication)getActivity().getApplication();

        userNameTv.setText(application.loginResult.getUserName());

//        userPayTv.setText(application.loginResult.getWaitPayCount());
//        userReciveTv.setText(application.loginResult.getWaitReceiveCount());
    }

    @Override
    protected void  initController() {
        super.initController();
        mController = new LoginController(getActivity());
        mController.setIModeChangeListener(this);
    }

    @Override
    public void onModelChanged(int action, Object... valuves) {
        mHandler.obtainMessage(action, valuves[0]).sendToTarget();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.logout_btn: // 登录出去，发布异步请求
                mController.sendAsyncMessage(IdiyMessage.CLEAR_USER_ACTION, 0);
                break;
        }
    }
}
